<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 面包屑部分  crumbs/面包屑英文-->
        <div class="crumbs" >
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="subjects.id && subjects.name">
            <el-breadcrumb-item>面板</el-breadcrumb-item>
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ subjects.id ? subjects.name : '' }}</el-breadcrumb-item>
            <el-breadcrumb-item>标签</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <!-- 搜索框部分 srarch-box/搜索框英文 -->
        <div class="search-box">
          <el-form ref="form" :model="form" label-width="80px">
            <el-row>
              <el-col :span="6">
                <el-form-item label="标签名称">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="状态">
                  <el-select v-model="form.region" placeholder="请选择">
                    <el-option label="禁用" value="0"></el-option>

                    <el-option label="启用" value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="5" :pull="1">
                <el-form-item>
                  <el-button @click.native="resetFn">清除</el-button>
                  <el-button type="primary" @click="searchFn">搜索</el-button>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item class="right">
                  <el-button  v-if="subjects.id && subjects.name"  type="text" class="colright" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
                  <el-button type="success" icon="el-icon-edit" @click="addTags"
                    >新增数据</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <!-- 数据条部分 data/数据英文 -->
        <div class="data">
           <el-alert show-icon type="info" close-text=" ">
            <slot>数据一共{{ total }}条</slot>
          </el-alert>
        </div>
        <!-- 表格组件  table/表格英文-->
        <div class="table">
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="序号" width="180">
            </el-table-column>
            <el-table-column prop="subjectName" label="所属学科" width="180">
            </el-table-column>
            <el-table-column prop="tagName" label="标签名称"> </el-table-column>
            <el-table-column prop="username" label="创建者"> </el-table-column>
            <el-table-column prop="addDate" label="创建日期">
              <template #default="{ row }">
                {{ formatDate(row.addDate) }}
              </template>
            </el-table-column>
            <el-table-column prop="state" label="状态">
              <template #default="{ row }">
                {{ row.state === 1 ? "已启用" : "已禁用" }}
              </template>
            </el-table-column>
            <el-table-column prop="state" label="操作">
              <template #default="{ row }">
                <el-button type="text" @click="isOk(row)">{{
                  row.state === 1 ? "禁用" : "启用"
                }}</el-button>
                <el-button
                  type="text"
                  :disabled="row.state === 1"
                  @click="revise(row)"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  :disabled="row.state === 1"
                  @click="remove(row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页组件 -->
        <div class="paging">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="query.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size.sync="query.pagesize"
            layout=" prev, pager, next,sizes, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
        <!-- 新增弹层组件 -->
        <TagsAdd
          :dialogVisible="dialogVisible"
          @close="close"
          :resForm="resForm"
        ></TagsAdd>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入查询列表接口
import { list, remove, changeState } from '@/api/hmmm/tags.js'
// 引入添加组件
import TagsAdd from '../components/tags-add.vue'
// 引入时间格式化包
import dayjs from 'dayjs'
export default {
  components: {
    TagsAdd
  },
  data () {
    return {
      // 修改弹层父传子数据
      resForm: {
        // 所属学科
        subjectID: '',
        // 目录名称
        tagName: '',
        // 参数id
        id: ''
      },
      // 总条数
      total: null,
      // 新增弹层显隐
      dialogVisible: false,
      // 启用禁用按钮，默认禁用
      disable: false,
      query: {
        // 当前页
        page: 1,
        // 每页显示多少条
        pagesize: 10
      },
      // 表单绑定数据
      form: {
        // 标签名称
        name: '',
        // 状态
        region: ''
      },
      // 表格绑定数据
      tableData: []
    }
  },
  // 生命周期创建时获取信息
  created () {
    // 获取列表
    this.getList()
  },
  // 计算属性获取路由参数query方式传参拼接字符串形式
  computed: {
    subjects () {
      return this.$route.query || null
    }
  },
  // 监听路由参数的变化
  watch: {
    '$route.query' () {
      // 有变化重新获取列表
      this.getList()
    }
  },
  methods: {
    // 获取标签列表
    async getList () {
      const tt = await list(this.query)
      console.log('标签列表', tt)
      this.tableData = tt.data.items
      // 总条数
      this.total = tt.data.counts
    },
    // 分页组件方法 每页多少条
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.getList()
    },
    // 分页组件方法 当前页面
    handleCurrentChange (val) {
      this.query.page = val
      this.getList()
    },
    // 按钮禁用启用
    async isOk (val) {
      await changeState({ id: val.id, state: val.state === 0 ? '1' : '0' })
      this.getList()
    },
    // 新增弹层
    addTags () {
      this.dialogVisible = true
    },
    // 关闭弹层
    close (val) {
      this.dialogVisible = val
      this.resForm.id = null
      this.getList()
    },
    // 删除标签
    remove (res) {
      // 防止误删
      this.$confirm('确认删除？')
        .then(async (_) => {
          // 调用接口
          await remove(res)
          // 提示信息
          this.$message('删除成功')
          // 重新获取
          this.getList()
        })
        .catch((_) => {})
    },
    // 修改打开弹层回显数据
    revise (res) {
      this.dialogVisible = true
      this.resForm.subjectID = res.subjectID
      this.resForm.tagName = res.tagName
      this.resForm.id = res.id
    },
    // 时间格式化
    // str='默认值' str如果不传 读取默认值'YYYY-MM-DD'
    formatDate (value, str = 'YYYY-MM-DD hh:mm:ss') {
      return dayjs(value).format(str)
    },
    // 清除按钮清除上面的输入框
    resetFn () {
      this.form = {
        // 标签名称
        name: '',
        // 状态
        region: ''
      }
      this.getList()
    },
    // 搜索按钮搜索
    async searchFn () {
      const sou = await list({
        ...this.query,
        tagName: this.form.name,
        state: this.form.region
      })
      this.tableData = sou.data.items
    }
  }
}
</script>

<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.crumbs{
  margin-bottom: 5px;
}
.right{
float: right;
}
</style>
